/* 用户相关的vuex操作 */
import { getUserInfo } from '@/api/user'
import { setStorageToken, getStorageToken, removeStorageToken } from '@/utils/storage'
export default {
  state: {
    token: getStorageToken(),
    userinfo: {}
  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      // 并将token存到本地存储里面
      // localStorage.setItem('yd3-token', JSON.stringify(payload))
      setStorageToken(payload)
    },
    removeToken(state) {
      // 清除掉vuex中的token 和本地存储中的token
      state.token = {}
      removeStorageToken()
    },
    setUserInfo(state, payload) {
      state.userinfo = payload
    }
  },
  actions: {
    async setUserInfoAsync(context) {
      try {
        const res = await getUserInfo()
        context.commit('setUserInfo', res.data)
      } catch {
        this.$toast('获取用户信息失败')
      }
    }
  },
  namespaced: true
}
